package hapi.geom;

import hapi.math.Vector2D;

class Circle {
	
	public var center:Vector2D;
	public var radius:Float;
	
	public function new(center, radius){
		this.center = center;
		this.radius = radius;
	}
	
	public inline function contains(p){
		return center.distance(p) <= radius;
	}
	
	public function closest(to){
		var d = center.minusNew(to);
		var dl = d.getLength();
		if( dl <= radius )
			return to.clone();
		else
			return to.plusNew(d.mult((dl - radius)/dl));
	}
	
	public function intersectionVector(c:Circle):Vector2D {
		var v = c.center.minusNew( center );
		var d = c.radius + radius - v.getLength();
		return v.getNormal().mult(d);
	}
	
	public function intersect(c):Bool {
		return center.distance( c.center ) < (radius + c.radius);
	}
	
	public function clone():Circle {
		return new Circle(center.clone(), radius);
	}
	
	public function toString(){
		return "Circle("+center+","+radius+")";
	}
}